# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 

class Theme
  attr_reader :name, :path
  
  def initialize(name, path)   
    @name, @path = name, path   
  end   
  
  def layout #模板文件,在layouts文件里，叫default.html.erb   
    "../../themes/#{name}/layouts/default.rhtml"   
  end   
  
  def self.find(name) #查找指定(name)的Theme   
    self.new(name,theme_path(name))     
  end   
  
  def self.themes_root #所有theme的root   
    RAILS_ROOT + "/themes"   
  end   
  
  def self.theme_path(name) #指定(name)theme的path   
    themes_root + "/" + name   
  end   
  
  def self.theme_from_path(path) #查找指定(path)的Theme   
    name = path.scan(/[-\w]+$/i).flatten.first   
    self.new(name, path)   
  end   
  
  def self.find_all #查找所有Theme   
    installed_themes.inject([]) do |array, path|   
      array << theme_from_path(path)    
    end   
  end   
  
  def self.installed_themes #载入theme   
    search_theme_directory   
  end   
  
  def self.search_theme_directory #读入   
    glob = "#{themes_root}/[a-zA-Z0-9]*"   
    Dir.glob(glob).select do |file|   
      File.readable?("#{file}/about.markdown")   
    end.compact   
  end
end   
